package com.portalEsporteClubeBahia.app.bo;

import java.util.List;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.dao.TeamDAO;
import com.portalEsporteClubeBahia.app.vo.TeamVO;

public class TeamBO {
	
	private TeamDAO teamDAO;
	
	public TeamBO() throws ApplicationException{
		try{
			
			teamDAO = new TeamDAO();
			
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public TeamVO findByPrimaryKey(TeamVO filter) throws ApplicationException{
		try{
			return teamDAO.findByPrimaryKey(filter);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public List<TeamVO> findAll() throws ApplicationException{
		try{
			return teamDAO.findAll();
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}

	public List<TeamVO> findByFilter(TeamVO filter) throws ApplicationException{
		try{
			return teamDAO.findByFilter(filter);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public TeamVO findByShortNameId(TeamVO filter) throws ApplicationException{
		try{
			if(filter.getId()==null){
				throw new ApplicationException("MISSING ID!");
			}
			if(filter.getShortName()==null || filter.getShortName().equals("")){
				throw new ApplicationException("MISSING SHORTNAME!");
			}
			return teamDAO.findByShortNameId(filter);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public void insert(TeamVO team) throws ApplicationException{
		try{
			
			if(team.getName()==null || team.getName().equals("")){
				throw new ApplicationException("MISSING NAME!");
			}
			if(team.getShortName()==null || team.getShortName().equals("")){
				throw new ApplicationException("MISSING SHORTNAME!");
			}
			if(team.getUrlImage()==null || team.getUrlImage().equals("")){
				throw new ApplicationException("MISSING URL IMAGE!");
			}
			
			teamDAO.insert(team);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public void update(TeamVO team) throws ApplicationException{
		try{
			
			if(team.getId()==null){
				throw new ApplicationException("MISSING ID!");
			}
			if(team.getName()==null || team.getName().equals("")){
				throw new ApplicationException("MISSING NAME!");
			}
			if(team.getShortName()==null || team.getShortName().equals("")){
				throw new ApplicationException("MISSING SHORTNAME!");
			}
			if(team.getUrlImage()==null || team.getUrlImage().equals("")){
				throw new ApplicationException("MISSING URL IMAGE!");
			}
			
			teamDAO.update(team);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}

	public void delete(TeamVO team) throws ApplicationException{
		try{
			
			if(team.getId()==null){
				throw new ApplicationException("MISSING ID!");
			}
			
			teamDAO.delete(team);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
}
